package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbHouseInfo;
import com.ruoyi.system.mapper.TbHouseMapper;
import com.ruoyi.system.domain.TbHouse;
import com.ruoyi.system.service.ITbHouseService;

/**
 * 房屋管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class TbHouseServiceImpl implements ITbHouseService 
{
    @Autowired
    private TbHouseMapper tbHouseMapper;

    /**
     * 查询房屋管理
     * 
     * @param id 房屋管理主键
     * @return 房屋管理
     */
    @Override
    public TbHouse selectTbHouseById(Long id)
    {
        return tbHouseMapper.selectTbHouseById(id);
    }

    /**
     * 查询房屋管理列表
     * 
     * @param tbHouse 房屋管理
     * @return 房屋管理
     */
    @Override
    public List<TbHouse> selectTbHouseList(TbHouse tbHouse)
    {
        return tbHouseMapper.selectTbHouseList(tbHouse);
    }

    /**
     * 新增房屋管理
     * 
     * @param tbHouse 房屋管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbHouse(TbHouse tbHouse)
    {
        tbHouse.setCreateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbHouse.getImages() != null && tbHouse.getImages().contains(",")) {
            tbHouse.setImages(tbHouse.getImages().split(",")[0]);
        }
        int rows = tbHouseMapper.insertTbHouse(tbHouse);
        insertTbHouseInfo(tbHouse);
        return rows;
    }

    /**
     * 修改房屋管理
     * 
     * @param tbHouse 房屋管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbHouse(TbHouse tbHouse)
    {
        tbHouse.setUpdateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbHouse.getImages() != null && tbHouse.getImages().contains(",")) {
            tbHouse.setImages(tbHouse.getImages().split(",")[0]);
        }
        tbHouseMapper.deleteTbHouseInfoByHouseId(tbHouse.getId());
        insertTbHouseInfo(tbHouse);
        return tbHouseMapper.updateTbHouse(tbHouse);
    }

    /**
     * 批量删除房屋管理
     * 
     * @param ids 需要删除的房屋管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbHouseByIds(Long[] ids)
    {
        tbHouseMapper.deleteTbHouseInfoByHouseIds(ids);
        return tbHouseMapper.deleteTbHouseByIds(ids);
    }

    /**
     * 删除房屋管理信息
     * 
     * @param id 房屋管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbHouseById(Long id)
    {
        tbHouseMapper.deleteTbHouseInfoByHouseId(id);
        return tbHouseMapper.deleteTbHouseById(id);
    }

    /**
     * 新增房屋详情信息
     * 
     * @param tbHouse 房屋管理对象
     */
    public void insertTbHouseInfo(TbHouse tbHouse)
    {
        List<TbHouseInfo> tbHouseInfoList = tbHouse.getTbHouseInfoList();
        Long id = tbHouse.getId();
        if (StringUtils.isNotNull(tbHouseInfoList))
        {
            List<TbHouseInfo> list = new ArrayList<TbHouseInfo>();
            for (TbHouseInfo tbHouseInfo : tbHouseInfoList)
            {
                tbHouseInfo.setHouseId(id);
                list.add(tbHouseInfo);
            }
            if (list.size() > 0)
            {
                tbHouseMapper.batchTbHouseInfo(list);
            }
        }
    }
}
